#include "TabWidget.h"
#include "CampaignModellingForm.h"
#include "CharacterModellingForm.h"


#include <ItemModel.h>
#include <ItemRoot.h>
#include <CampaignItem.h>
#include <CharacterItem.h>
#include <FileHandler.h>



#include <QTabBar>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QTimer>



TabWidget::TabWidget(QWidget *parent) :
    QTabWidget(parent)
{
    QFile styleFile(":/styles/style");
    if (styleFile.open(QFile::ReadOnly | QFile::Text))
        setStyleSheet(styleFile.readAll());
    else
        qDebug("Failed to find style");
}


AbstractModellingForm* TabWidget::modellingForm(int index) const
{
    return dynamic_cast<AbstractModellingForm*>(widget(index));
}

void TabWidget::onNew(int type)
{
    // todo: open template from source

    ItemRoot* root = 0;
    switch (type)
    {
    case CharacterTab:
        root = new CharacterItem(tr("New Character"), this);
        break;
    case CampaignTab:
        root = new CampaignItem(tr("New Campaign"), this);
        break;
    default:
        return;
    }

    if (!root) return;

    root->setDirty(true);
    onCreate(root);
}



void TabWidget::onOpen()
{
    qDebug(Q_FUNC_INFO);

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open"), QDir::currentPath(), tr("Xml Files (*.xml)"));
    if (fileName.isEmpty()) return;

    ItemRoot* root = FileHandler::open(fileName);
    if (!root)
    {
        QMessageBox::information(0, tr("Open Failed"), tr("Failed to open file!"));
        return;
    }

    onCreate(root);
    root->setDirty(false);
}



void TabWidget::onSave(int index)
{
    if (index < 0) index = currentIndex();

    AbstractModellingForm* form = modellingForm(index);
    if (!form) return;

    ItemModel* model = qobject_cast<ItemModel*>(form->model());
    if (!model) return;

    ItemRoot* item = qobject_cast<ItemRoot*>(model->rootItem());
    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist!"));
        return;
    }

    if (!QFile::exists(item->filePath()))
    {
        onSaveAs(index);
        return;
    }

    if (!FileHandler::save(item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setDirty(false);
}



void TabWidget::onSaveAs(int index)
{
    if (index < 0) index = currentIndex();

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (fileName.isEmpty()) return;

    AbstractModellingForm* form = modellingForm(index);
    if (!form) return;

    ItemModel* model = qobject_cast<ItemModel*>(form->model());
    if (!model) return;

    ItemRoot* item = qobject_cast<ItemRoot*>(model->rootItem());
    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist!"));
        return;
    }

    item->setFilePath(fileName);
    if (!FileHandler::save(item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setDirty(false);
}



void TabWidget::onClose(int index)
{
    if (index < 0) index = currentIndex();


    AbstractModellingForm* form = modellingForm(index);
    if (!form)
    {
        removeTab(index);
        return;
    }


    ItemModel* model = dynamic_cast<ItemModel*>(form->model());
    if (!model) return;


    ItemRoot* root = model->rootItem();
    if (!root || !root->isDirty())
    {
        removeTab(index);
        return;
    }


    int q = QMessageBox::question(0,
                                 tr("Save Changed?"),
                                 tr("Campaign has changed. Do you want to save it?"),
                                 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

    switch (q)
    {
    case QMessageBox::Cancel:
        return;

    case QMessageBox::No:
        break;

    case QMessageBox::Yes:
        onSave(index);
        break;
    }

    removeTab(index);
}



void TabWidget::onAdd()
{
    AbstractModellingForm* form = modellingForm(currentIndex());
    if (!form) return;
    form->onAction(AbstractModellingForm::AddAction);
}



void TabWidget::onDelete()
{
    AbstractModellingForm* form = modellingForm(currentIndex());
    if (!form) return;
    form->onAction(AbstractModellingForm::DeleteAction);
}



void TabWidget::onCreateGroup()
{
    AbstractModellingForm* form = modellingForm(currentIndex());
    if (!form) return;

    ItemModel* model = form->model();
    if (!model) return;


}



void TabWidget::onCreateItem()
{
    AbstractModellingForm* form = modellingForm(currentIndex());
    if (!form) return;

    ItemModel* model = form->model();
    if (!model) return;
}



void TabWidget::onCreateReference()
{
    AbstractModellingForm* form = modellingForm(currentIndex());
    if (!form) return;

    ItemModel* model = form->model();
    if (!model) return;
}



void TabWidget::onCreate(ItemRoot *item)
{
    qDebug(Q_FUNC_INFO);

    if (!item) return;

    QWidget* widget = 0;
    if (dynamic_cast<CampaignItem*>(item))
    {
        widget = new CampaignModellingForm(new ItemModel(item, this), this);
    }

    else if (dynamic_cast<CharacterItem*>(item))
    {
        qDebug("Character form");
        CharacterModellingForm* form = new CharacterModellingForm(this);
        form->setModel(new ItemModel(item, form));

        widget = form;
    }

    else
    {
        return;
    }

    addTab(widget, item->name());
    setCurrentWidget(widget);
}



